#include "projectHeader.h"

void addInstructorInfo ( instructor **ins )
{
    instructor *walkInsTemp = NULL;
    walkInsTemp = ( instructor * ) malloc ( sizeof ( instructor ) );

    printf ( "Give instructor's name: \n" );
    scanf ( "%s", walkInsTemp->cName );
    walkInsTemp->next = (*ins);
    (*ins) = walkInsTemp;
    nCountIns ++;
}

void changeInstructorInfo ( instructor **ins )
{
    char cName[64];
    instructor *walkInsTemp, *holdInsTemp, *insTemp;
    makeInsEmpty ( &insTemp );

    printf ( "Enter the instructor name: " );
    scanf ( "%s", cName );

    if ( isInstructor ( cName, (*ins) ) )
    {
        for ( holdInsTemp = (*ins);
              strcmp ( holdInsTemp->cName, cName );
              holdInsTemp = holdInsTemp->next )
        {
            walkInsTemp = ( instructor * ) malloc ( sizeof ( instructor ) );
            strcpy ( walkInsTemp->cName, holdInsTemp->cName );
            walkInsTemp->next = insTemp;
            insTemp = walkInsTemp;
        }
        /* pop out info and push into temp */

        printf ( "Enter new name: \n" );
        scanf ( "%s", holdInsTemp->cName );
        /* change info */

        for ( insTemp = insTemp->next; insTemp != NULL; insTemp = insTemp->next )
        {
            walkInsTemp = ( instructor * ) malloc ( sizeof ( instructor ) );
            strcpy ( walkInsTemp->cName, insTemp->cName );
            walkInsTemp->next = holdInsTemp;
            holdInsTemp = walkInsTemp;
        }
        /* push back to hold */

        (*ins) = holdInsTemp;
        printf ( "Change successful!\n" );
    }
    else
        printf ( "Warning: Did not find this instructor\n" );
}

void clearInstructor ( instructor **ins )
{
    int i;
    instructor *dltTemp;

    for ( i = 0; i < nCountStu; i ++ )
    {
        dltTemp = (*ins);
        (*ins) = (*ins)->next;
        free ( dltTemp );
    }
}
/* clear instructor */

void deleteInstructor ( instructor **ins )
{
    char cName[64];
    instructor *walkInsTemp, *holdInsTemp, *dltTemp, *insTemp;
    makeInsEmpty ( &insTemp );

    printf ( "Enter the instructor's name: " );
    scanf ( "%s", cName );

    if ( isInstructor ( cName, *ins ) )
    {
        for ( holdInsTemp = (*ins);
              strcmp ( holdInsTemp->cName, cName );
              holdInsTemp = holdInsTemp->next )
        {
            walkInsTemp = ( instructor * ) malloc ( sizeof ( instructor ) );
            strcpy ( walkInsTemp->cName, holdInsTemp->cName );
            walkInsTemp->next = insTemp;
            insTemp = walkInsTemp;
        }
        /* pop out info and push into temp */

        dltTemp = holdInsTemp;
        holdInsTemp = holdInsTemp->next;
        free ( dltTemp );
        nCountIns --;
        /* delete instructor */

        for ( insTemp = insTemp->next; insTemp != NULL; insTemp = insTemp->next )
        {
            walkInsTemp = ( instructor * ) malloc ( sizeof ( instructor ) );
            strcpy ( walkInsTemp->cName, holdInsTemp->cName );
            walkInsTemp->next = holdInsTemp;
            holdInsTemp = walkInsTemp;
        }
        /* push back to hold */

        (*ins) = holdInsTemp;
        printf ( "Delete successful!\n" );
    }
    else
        printf ( "Warning: Did not find this instructor\n" );
}

int isInstructor ( char *c, instructor *ins )
{
    int i;
    char cTemp[64];

    for ( i = 0;
          strcmp ( cTemp, c ) && i < nCountIns;
          ins = ins->next )
    {
        strcpy ( cTemp, ins->cName );
        i ++;
    }

    if ( strcmp ( cTemp, c ) )
        return 0;
    else
        return 1;
}

double getAverageScore ( student *stu )
{
    double nTotal = 0, i;

    for ( i = 0; i < nCountStu; i ++ )
    {
        nTotal += stu->nScore;
        stu = stu->next;
    }

    return ( nTotal / nCountStu );
    /* get average score */
}

void makeInsEmpty ( instructor **ins )
{
    instructor *walkInsTemp;
    walkInsTemp = ( instructor * ) malloc ( sizeof ( instructor ) );
    strcpy ( walkInsTemp->cName, "" );
    walkInsTemp->next = NULL;
    (*ins) = walkInsTemp;
}

instructor * readInstructors ( FILE *f )
{
    instructor *walkInsTemp, *holdInsTemp;
    int i;

    makeInsEmpty ( &holdInsTemp );
    fscanf ( f, "%d", &nCountIns );

    for ( i = 0; i < nCountIns; i ++ )
    {
        walkInsTemp = ( instructor * ) malloc ( sizeof ( instructor ) );
        fscanf ( f, "%s", walkInsTemp->cName );
        walkInsTemp->next = holdInsTemp;
        holdInsTemp = walkInsTemp;
    }

    return holdInsTemp;
}

void saveInfo ( instructor *ins, student *stu )
{
    FILE *fileWriteIns, *fileWriteStu;
    int i;

    fileWriteIns = fopen ( "instructorInfo.txt", "w" );
    fileWriteStu = fopen ( "studentInfo.txt", "w" );

    fprintf ( fileWriteIns, "%d\n", nCountIns );
    fprintf ( fileWriteStu, "%d\n", nCountStu );
    /* print number of instructors and students in file */

    for ( i = 0; i < nCountIns; i ++ )
    {
        fprintf ( fileWriteIns, "%s\n", ins->cName );
        ins = ins->next;
    }
    /* save instructor info */

    sortStudents ( fileWriteStu, stu );
    /* sort students */

    printf ( "Data saved!\n" );
    fclose ( fileWriteIns );
    fclose ( fileWriteStu );
}

void showClass ( instructor *ins, student *stu )
{
    int i;
    student *walkStuTemp, *holdStuTemp;
    makeStuEmpty ( &holdStuTemp );

    printf ( "\nInstructor:\n");
    for ( i = 0; i < nCountIns; i ++ )
    {
        printf( "%s\n", ins->cName );
        ins = ins->next;
    }
    /* list all instructors */

    for ( i = 0; i < nCountStu; i ++ )
    {
        walkStuTemp = ( student * ) malloc ( sizeof ( student ) );
        strcpy ( walkStuTemp->cName, stu->cName );
        walkStuTemp->nScore = stu->nScore;
        walkStuTemp->next = holdStuTemp;
        holdStuTemp = walkStuTemp;
        stu = stu->next;
    }

    printf ( "\n\nStudent:\n" );
    for ( i = 0; i < nCountStu; i ++ )
    {
        printf ( "%s\t%.1lf\t", holdStuTemp->cName, holdStuTemp->nScore );
        holdStuTemp = holdStuTemp->next;
    }
    /* list all students */

    printf ( "\n" );
}

